<template>
  <div style="min-width: 1200px">
    <el-button type="primary" icon="el-icon-plus" @click="addLogoPhoto"
      >添加</el-button
    >
    <el-table :data="trademarkList" border style="margin: 20px 0">
      <el-table-column type="index" label="序号" prop="#"></el-table-column>
      <el-table-column label="品牌名称" prop="tmName"></el-table-column>
      <el-table-column label="品牌LOGO">
        <template slot-scope="scope">
          <div style="width: 60px; height: 60px">
            <img
              style="width: 60px; height: 60px border-radius: 8px;"
              :src="scope.row.logoUrl"
              alt=""
            />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="warning"
            icon="el-icon-edit"
            size="mini"
            @click="updateTrademark(scope.row)"
            >修改</el-button
          >
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="deleteTrademark(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="margin: 20px 0; text-align: center"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[3, 5, 7, 9]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 点击添加弹出层 -->
    <el-dialog
      :title="!tmForm.id ? '添加品牌' : '修改品牌'"
      :visible.sync="addPhotoDialogVisible"
      width="50%"
      :before-close="handleClose"
      @closed="addPhotoDialogVisibleClosed"
    >
      <el-form :model="tmForm" :rules="rule" ref="tmForm" label-width="120px">
        <el-form-item label="品牌名称" prop="tmName">
          <el-input v-model="tmForm.tmName"></el-input>
        </el-form-item>
        <el-form-item label="上传品牌LOGO" prop="logoUrl" label-width="120px">
          <el-upload
            class="avatar-uploader"
            action="/dev-api/admin/product/fileUpload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="tmForm.logoUrl" :src="tmForm.logoUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <div class="el-upload__tip" slot="tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addPhotoDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addTrademark">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      trademarkList: [],
      page: 1,
      limit: 3,
      total: 0,
      addPhotoDialogVisible: false,
      rule: {
        tmName: [
          { required: true, message: "请输入品牌名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 2 到 10 个字符", trigger: "blur" },
        ],
        logoUrl: [{ required: true, message: "请上传LOGO图片" }],
      },
      tmForm: {
        tmName: "",
        logoUrl: "",
      },
    };
  },
  created() {
    this.getTrademarktList();
  },
  methods: {
    async getTrademarktList() {
      const res = await this.$Api.trademark.reqGetBrandList(
        this.page,
        this.limit
      );
      this.trademarkList = res.data.records;
      this.total = res.data.total;
    },
    // 每次显示多少条数据
    handleSizeChange(newlimit) {
      this.limit = newlimit;
      this.getTrademarktList();
    },
    // 页码切换
    handleCurrentChange(newpage) {
      this.page = newpage;
      this.getTrademarktList();
    },
    addLogoPhoto() {
      this.addPhotoDialogVisible = true;
    },
    // 关闭窗口
    handleClose() {
      this.addPhotoDialogVisible = false;
    },
    // 图片上传成功的回调
    handleAvatarSuccess(res, file) {
      this.tmForm.logoUrl = URL.createObjectURL(file.raw);
    },
    // 图片上传之前
    beforeAvatarUpload(file) {
      const isPNG = file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isPNG) {
        this.$message.error("上传LOGO图片只能是PNG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isPNG && isLt2M;
    },
    // 窗口关闭
    addPhotoDialogVisibleClosed() {
      this.$nextTick(() => {
        this.$refs.tmForm.resetFields();
      });
    },
    addTrademark() {
      this.$refs.tmForm.validate(async (valid) => {
        if (!valid) return;
        this.addPhotoDialogVisible = false;
        const res = await this.$Api.trademark.reqAddOrUpdateTrademark(
          this.tmForm
        );
        if (res.code == 200) {
          this.$message.success(
            !this.tmForm.id ? "新增品牌成功!" : "修改品牌成功!"
          );
        }
        this.getTrademarktList();
      });
    },
    async updateTrademark(row) {
      this.addPhotoDialogVisible = true;
      this.tmForm = { ...row };
    },
    async deleteTrademark(row) {
      const confirm = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (confirm == "confirm") {
        const res = await this.$Api.trademark.reqDeleteTrademark(row.id);
        if (res.code == 200) this.$message.success("删除商品成功!");
        this.getTrademarktList();
      }
    },
  },
};
</script>

<style lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
